package com.example.crawler.service.impl;

import com.example.crawler.model.Img;
import com.example.crawler.repository.ImgRepository;
import com.example.crawler.service.ImgService;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import utils.CrawlerUtils;
import utils.ParsePageUtils;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.stream.Collectors;

/**
 * @author pq
 * @date 2022/10/28 13:52
 * @description
 * @modified
 **/
@Slf4j
@Service
public class ImgServiceImpl implements ImgService {

    static Queue<String> urlQueue = new ConcurrentLinkedQueue<>();

    static Queue<String> imgUrlQueue = new ConcurrentLinkedQueue<>();

    static String url = "https://www.ecjtuit.edu.cn/xqzl/xyfg1/xyfg.htm";

    static boolean first = true;

    @Resource
    private ImgRepository imgRepository;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void getImg() {
        //获取网页链接
        getPageLinks();
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        //获取图片链接
        getImgUrl();
        //下载图片
//        saveImg();
    }



    private static void getPageLinks() {
            new Thread(() -> {
                while (true) {
                    try {
                        if (urlQueue.size() == 0 && first) {
                            String pageStr = ParsePageUtils.parsePageToString(url);
                            urlQueue.addAll(ParsePageUtils.getPageLink(pageStr));
                            first = false;
                        } else {
                            String pageStr = ParsePageUtils.parsePageToString(urlQueue.poll());
                            urlQueue.addAll(ParsePageUtils.getPageLink(pageStr));
//                            System.out.println("====================================网页" + urlQueue);
                        }
                        Queue<String> tempUrlQueue = new ConcurrentLinkedQueue<>(urlQueue.stream().distinct().collect(Collectors.toList()));
                        urlQueue.removeAll(urlQueue);
                        urlQueue = tempUrlQueue;
                        urlQueue.stream().distinct().collect(Collectors.toList());
                    } catch (Exception e) {
                        log.error(e.getClass().getSimpleName(), e, "获取链接异常");
                    }
                }
            }).start();
    }

    private static void getImgUrl() {
            new Thread(() -> {
                while (true) {
                    try {
                        String url = urlQueue.poll();
                        String pageStr1 = "";
                        if (StringUtils.isNotEmpty(url)) {
                            pageStr1 = ParsePageUtils.parsePageToString(url);
                        }
                        List<String> imgUrls = null;
                        imgUrls = ParsePageUtils.getImgUrls(pageStr1);
                        System.out.println("图片："+imgUrls);
                        if (Objects.nonNull(imgUrls) && imgUrls.size() != 0) {
                            imgUrlQueue.addAll(imgUrls);
//                            Queue<String> tempImgUrlQueue = new LinkedList();
//                            tempImgUrlQueue.addAll(imgUrlQueue.stream().distinct().collect(Collectors.toList()));
//                            imgUrlQueue.removeAll(imgUrlQueue);
//                            imgUrlQueue = tempImgUrlQueue;
                        }
//                        System.out.println("==========================图片" + imgUrlQueue);
                    } catch (Exception e) {
                        log.error("解析网页链接错误：{}", e.getMessage(), e);
                    }
                }
            }).start();
    }

    private static void saveImg() {
            new Thread(() -> {
                for (int j = 0; j < 100; j++) {
                    try {
                        String imgUrl = "";
                        if (StringUtils.isNotEmpty(imgUrl = imgUrlQueue.poll())) {
                            ParsePageUtils.downloadImage(imgUrl, CrawlerUtils.getUUID(), "C:\\Users\\pq\\Desktop\\图片下载");
                        }
                    }catch (Exception e){
                        log.error("下载图片错误：{}", e.getMessage(), e);
                    }
                }
            }).start();
    }

    public Queue<String> getUrlQueue(){
        return urlQueue;
    }

    public Queue<String> getImgUrlQueue() {
        return imgUrlQueue;
    }

    @Override
    public List<String> getImgUrls() {
        List<String> imgUrlList= Lists.newArrayList();
        imgRepository.findAll().forEach(img->{
            imgUrlList.add(img.getUrl());
        });
        return imgUrlList;
    }
}
